package communication.game;
import java.util.Date;

import game.MinimalGame;
import game.ServersideGame;
import communication.ComCliSrv;

import server.ServerInternals;
/**
 * Signal de fin de la partie
 * @author Julien Roch
 * @version 0.1
 */
public class StopGame extends ComCliSrv {
	
	private int id;
	/**
	 * Pour la serialisation
	 */
	private static final long serialVersionUID = -3615938511709747635L;

	/**
	 * Termine une partie
	 * @param id identifiant de la partie a terminer
	 */
	public StopGame(int id){
		this.id = id;
		
	}
	@Override
	public void handleServerInternals(ServerInternals internals) {
		internals.stopGame(this.id);
		
		ServersideGame ssg = internals.getGame(this.id);
		
		MinimalGame mg = new MinimalGame(ssg);
		String players[] = ssg.getPlayers();
		String scores[] = new String[players.length];
		if(ssg.getTimer()!=null)
			ssg.getTimer().cancel();
		for(int i=0;i<players.length;i++){
			scores[i] = ssg.getScores().get(players[i]).toString();
		}
		internals.sendDataToClients(ssg.getPlayersSockets(), new ConfirmGameFinished(mg, (int)(new Date().getTime()-ssg.getStart())/1000, ssg.getPlayersAsPlayer()));
	}
}
